package com.frame.work.mqtt.data.handle;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.frame.work.entity.TblDevice;
import com.frame.work.entity.relation.BeijingYingYue;
import com.frame.work.mapper.TblDeviceMapper;
import com.frame.work.mqtt.util.MqttUtil;
import com.frame.work.service.TblDeviceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Component
public class MqttDataHandle {
    @Autowired
    private MqttUtil mqttUtil;
    @Autowired
    private TblDeviceService deviceService;
    @Autowired
    private TblDeviceMapper tblDeviceMapper;


    /**
     * 无数据库操作基础推送
     *
     * @param topic
     * @param message
     */

    public void basePush(String topic, String message) {
        mqttUtil.publish(topic, message, 0);
    }

    /**
     * mqtt既推送也操作Action数据库
     *
     * @param topic
     * @param message
     */
    public void mqttPush(String topic, String message) {
        try {
            basePush(topic, message);
            actionHandle(topic, message);
        } catch (Exception e) {
            System.out.println("mqttPush推送方法有异常：" + e.getMessage());
        }

    }

    /**
     * mqtt既推送也操作Action数据库
     *
     * @param topic
     * @param message
     */
    public void mqttPushModifyAction(String topic, String message) {
        try {
            basePush(topic, message);
            HashMap deviceMap = JSON.parseObject(message, HashMap.class);
            deviceService.updateDevice(deviceMap, topic);
        } catch (Exception e) {
            System.out.println("mqttPush推送方法有异常：" + e.getMessage());
        }

    }

    /**
     * 操作Action数据库，不推送
     *
     * @param topic
     * @param message
     */
    public void actionHandle(String topic, String message) {
        try {
            HashMap deviceMap = JSON.parseObject(message, HashMap.class);
            if (StringUtils.indexOf(topic, "_param") < 0 && (deviceMap.get("switchState") != null || deviceMap.get("actions") != null)) {
                deviceService.updateDevice(deviceMap, topic);
            }
            if (StringUtils.indexOf(topic, "_Fhwise") > 0) {
                BeijingYingYue objectMap = tblDeviceMapper.selectDeviceBejingYingYue(deviceMap.get("deviceMac").toString());
                // 设备存在
                if (objectMap != null && deviceMap.get("gatewayMac").equals(objectMap.getGatewayMac())) {
                    TblDevice tblDevice = new TblDevice();
                    tblDevice.setLwbz("0");
                    tblDevice.setSwitchTime(LocalDateTime.now());
                    tblDevice.setId(objectMap.getId());
                    tblDeviceMapper.updateById(tblDevice);
                }
            }
        } catch (Exception e) {
            System.out.println("Action操作系统异常" + e);
        }
    }

    /**
     * 设备离线重连
     *
     * @param topic
     * @param message
     */
    public void offlineReconnection(String topic, String message) {
        basePush(topic, message);
    }


    public void mqttPushLianKong_new(String topic, Object message) {
        Map<String, Object> deviceMap = (Map) JSONObject.parse(message.toString());
        deviceService.updateDevice(deviceMap, topic);
    }


    public void mqttPushLianKong(String topic, Object message) {
        //通过条件设备mac和类型查询设备和网关信息
        Map<String, Object> deviceMap = (Map) JSONObject.parse(message.toString());
        //之前的
        //Map<String, Object> deviceMap = (Map<String, Object>) JSONObject.toJSON(message);
        deviceService.lianKongDeviceHandle(deviceMap, topic);
    }

    public void pushScene(String topic, Object message) {
        basePush(topic, JSONObject.toJSONString(message));
        Map<String, Object> messageObj = (Map<String, Object>) message;
        if (topic.indexOf("_param") < 0 && (StringUtils.isNotEmpty(messageObj.get("switchState").toString()) || messageObj.get("actions") != null)) {
            deviceService.updateDeviceScene(messageObj);
        }
    }
}
